<?php
class AuthorityController extends IndexController
{
	public function init()
    {
		parent::init();
        $this->utl            = Yaf_Registry::get("utl");
        $this->AuthorityModel = new AuthorityModel();
        $this->Session        = Yaf_Registry::get("session");
        $this->UserLogsModel   = new LogsModel();
    }

    public function listAction() 
    {
		$offset            = 10;
		$page              = $this->getRequest()->getParam("page",1);
		$startLimit        = $offset*($page-1);
		$allAuthority_info = $this->AuthorityModel->getAuthorityPageInfo($startLimit,$offset);
		$allCounts         = $this->AuthorityModel->getAllCount();
        $allpage=$this->utl::page('/Authority/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $allAuthority_list     = array();
        foreach ($allAuthority_info AS $k => $v)
        {
            $allAuthority_list[$k] = $v;
            $allAuthority_list[$k]['rows'] = $allCounts[0]['rows'];
            $allAuthority_list[$k]['every_page'] = $offset;
        }
        $this->getView()->assign("allpage",$allpage);
        $this->getView()->assign("list",$allAuthority_list);
    }

    public function pageAction() 
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allAuthority_info = $this->AuthorityModel->getAuthorityPageInfo($startLimit,$offset);
        $allCounts         = $this->AuthorityModel->getAllCount();
        $allpage=$this->utl::page('/Authority/list', $page, $allCounts[0]['rows'], $offset, $status='');
        //$this->getView()->assign("allpage",$allpage);
        $this->getView()->assign("list",$allAuthority_info);
        exit(json_encode($allAuthority_info, true));
    }

    public function currentPageAction() 
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allAuthority_info = $this->AuthorityModel->getAuthorityPageInfo($startLimit,$offset);
        $allCounts         = $this->AuthorityModel->getAllCount();
        $allpage=$this->utl::page('/Authority/list', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);

    }

    public function delAction() 
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id)) 
        {
            $this->forward('Authority', 'list');
            return;
        }
        $rs = $this->AuthorityModel->del($id);
        $session_del = array();
        $user_info = $this->Session->user_info;
        foreach ($user_info as $k => $v) 
        {
            $session_del[$k]['user_id'] = $v['user_id'];
            $session_del[$k]['table_name'] = "authority";
            $session_del[$k]['action'] = "del";
        }
        $this->UserLogsModel->addUser($session_del);

        header('Content-type:application/json;charset:utf8');
        if (false === $rs) 
        {
            exit('{"status":false,"info":"删除失败！"}');
            return;
        } 
        exit('{"status":true,"info":"删除成功"}');
        return true;                                                                              
    }

    public function editAction() 
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id)) 
        {
            $this->forward('Authority', 'list');
            return;
        }
        if ($this->getRequest()->isPost()) 
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->AuthorityModel->edit($post, $id);
            $session_edit = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_edit[$k]['user_id'] = $v['user_id'];
                $session_edit[$k]['table_name'] = "authority";
                $session_edit[$k]['action'] = "edit";
            }
            $this->UserLogsModel->addUser($session_edit);
            if (false === $rs) 
            { 
                 exit('{"status":false,"info":"编辑失败！"}');
                 return;
            }     
            exit('{"status":true,"info":"编辑成功！"}');
        }
    }

    public function addAction() 
    {
        if ($this->getRequest()->isPost()) {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs = $this->AuthorityModel->add($post);
            $session_add = array();
            $user_info = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_add[$k]['user_id'] = $v['user_id'];
                $session_add[$k]['table_name'] = "authority";
                $session_add[$k]['action'] = "add";
            }
            $this->UserLogsModel->addUser($session_add);
            if (false === $rs) 
            {   
                exit('{"status":false,"info":"添加失败！"}');
                return;
            }
            exit('{"status":true,"info":"添加成功！"}');
        }
    }
}
?>